print("This will work")
L00: Jupyter basics
CELLS
Jupyter notebooks are split into “cells”.
If you click anywhere inside a notebook, it will enclose the cell that you clicked on in a rectangle, to show you the exact contents of that cell (the cell may be empty). - You can then press “Enter” to edit the contents of that cell (the enclosing rectangle will change colors - from blue to green). - When you are done editing a cell, you can press “Shift+Enter” to run it.
The two types of cells that you will be using most often are: - Markdown cells - Contain text in “Markdown” language (more on this below) - Convert a cell to Markdown by clicking on it, and the pressing “Esc” and “M” - Code cells - Contain code in language you specified by the notebook’s “kernel” (more on this below) - Convert a cell to Markdown by clicking on it, and the pressing “Esc” and “Y”
Markdown cells
These are cells where you write text. This cell is a Markdown cell. Think of Markdown as a lightweight version of Microsoft Word. It allows you to format your text (indentation, bullet points, italics, etc) using pre-specified syntax (code) rather than clicking through menu items like in Work.
Markdown formatting
The following markdown cell shows you how to do basic formatting in Markdown
Headers:
Largest header
Second largest header
Third largers header
Lists (bullet points):
- item
- subitem
- subsubitem
- subitem
- item
- subitem
- subsubitem
- subitem
Lists (numbered):
- item
- item
Lists (mixed):
- item
- subitem
- subsubitem
- subsubsubitem
- subsubitem
- subitem
- item
- subitem
- subsubitem
- subsubsubitem
- subsubitem
- subitem
This is how you do italics and this is how you do bold.
LaTex
Markdown also allows you to write mathematical formulas using LaTex syntax.
For example:
\(Y_{i,t} = \alpha + \beta X_{i,t} + \epsilon_{i,t}\)
Which was written as
$Y_{i,t} = \alpha + \beta X_{i,t} + \epsilon_{i,t}$
Note that the LaTex syntax for the symbols in your equation are different from Markdown syntax.
Code cells
By default, any new cell you create in a notebook will be a code cell.
In a code cell, you write code written in the language of the kernel you chose for your notebook. This is the language you selected when you created your notebook. It is usually displayed in the top right corner of the screen (under the “Logout” icon. If you write code written in any other language in a code cell, you will get errors when you try to run that cell.
For example, this is a Python 3 notebook, so the following code will work because it is written in the Python 3 language:
But the following code will not work, because it is written in the Stata language:
"This will not work" display
There are some kernels that will allow you to create notebooks that support multiple programming languages, but that is a more advanced topic and we will not cover it here.
Each code cell can contain multiple lines of code.
print("Line 1")
print("Line 2")
= 2 a
And the code in a particular code cell will recognize variables that were created in a different code cell (from above or below), as long as that cell has already been run. For example the following line of code knows what the variable “a” is (from the previous cell)
print(a)
To the left of every code cell, you will see the order in which you ran them in the current session. For example, “In [4]” means this is the fourth code cell you ran in this session. Because at any time, you can run any code cell you want, the order in which you run them may not match the order in which they appear in the notebook. I highly recommend you NEVER use in a code cell variables that you define in a cell that appears lower in the notebook. Another way of saying that is to always assume that the code cells will be run in the order in which they appear in the notebook (even though that need not be the case).
For example, don’t run the following cell AFTER you run the one after it:
print(b)
3
Note that code cells don’t always have an output. For example the line below does not seem to do anything when you run it, but it does assign the value 3 to variable b (we’ll talk more about variables next class).
= 3 b
Important notebook commands
As mentioned above, after clicking on any command, you can press “Enter” to enter “Edit mode” (to be able to edit the contents of that cell) or “Esc” to enter “Command mode” which allows you to use keyboard shortcuts to ask Jupyter to do specific things.
Here are some of the most common such commands (remember, these will not work unless you pressed “Esc” right before, to enter command mode):
- press “M” to convert the current cell to a Markdown cell
- press “Y” to convert the current cell to a Code cell
- press “A” to insert a new cells right above the current cell
- press “B” to insert a new cells below above the current cell
- press “C” to copy the selected cells
- press “X” to cut the selected cells
- press “V” to past the selected cells
- press “D” + “D” to delete selected cells
- press “Z” to undo cell deletion
- press “Ctrl” + “Enter” to run the selected cells
- press “0” (zero, not the letter O) to restart your kernel (this “forgets” all the code you ran in the current session)
To see all the available commands, press “Ctrl” + “Shift” + “P”